package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.fragment.CurrentWorkFragment;
import com.fancy.learncenter.ui.fragment.FinishedWorkFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CartoonMyWorkActivity extends BaseActivity implements View.OnClickListener,FinishedWorkFragment.OnFinishWorkListener{

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.cartoon_rope)
    ImageView cartoonRope;
    @Bind(R.id.current_work)
    ImageView currentWork;
    @Bind(R.id.finished_work_num)
    TextView finishedWorkNum;
    @Bind(R.id.finished_work)
    ImageView finishedWork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_my_work, Status.STATUS);
        ButterKnife.bind(this);
        initViewPage();
        initClick();
    }



    private void initViewPage() {

        List<Fragment> list = new ArrayList<>();
        list.add(new CurrentWorkFragment());
        list.add(new FinishedWorkFragment());

        List<String> titleList = new ArrayList<>();
        titleList.add("");
        titleList.add("");

        FragPagerAdapter adapter = new FragPagerAdapter(getSupportFragmentManager(), list, titleList);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                initIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initIndex(0);
    }


    private void initIndex(int index){
        viewPager.setCurrentItem(index);
        switch (index){
            case 0:
                currentWork.setImageResource(R.mipmap.cartoon_current_work_light);
                finishedWork.setImageResource(R.mipmap.cartoon_finished_work_gray);

                break;
            case 1:
                currentWork.setImageResource(R.mipmap.cartoon_current_work_gray);
                finishedWork.setImageResource(R.mipmap.cartoon_finished_work_light);
                break;
        }
    }

    private void initClick(){
        currentWork.setOnClickListener(this);
        finishedWork.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.current_work:
                initIndex(0);
                break;
            case R.id.finished_work:
                initIndex(1);
                break;
        }
    }

    @Override
    public void onFinishWork(int finished) {
        finishedWorkNum.setText("(" +String.valueOf(finished) + ")");
    }
}
